package cn.zxy.pagex.aop;

import cn.hutool.core.util.ObjUtil;


import com.github.pagehelper.PageHelper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

@Aspect
@Component
@EnableAspectJAutoProxy
public class PageXAop {
    private static final String PAGE_SIZE="pageSize";
    private static final String PAGE_Num="pageNum";
//    定义切入点
    @Pointcut("@annotation(cn.zxy.annotation.PageX)")
    public void point(){

    }

    @Around("point()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        String pageSize = request.getParameter("PAGE_SIZE");
        String pageNum = request.getParameter("PAGE_Num");
        if (ObjUtil.isNotEmpty(pageNum)&&ObjUtil.isNotEmpty(pageSize)){
            PageHelper.startPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        }

//        执行目标方法
        Object returnObject = joinPoint.proceed();
        return returnObject;
    }
}
